package com.example.neusoft.yuejian_12.Model;

import com.example.neusoft.yuejian_12.Interface.ContestFace;
import com.example.neusoft.yuejian_12.JavaBean.Contest;
import com.example.neusoft.yuejian_12.Listener.ContestDetailListener;
import com.example.neusoft.yuejian_12.Listener.ContestListener;
import com.example.neusoft.yuejian_12.Service.ContestService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by admin on 2017/10/11.
 */

public class ContestModel implements ContestFace {

    private Retrofit retrofit;
    private ContestService contestService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址*/

    public ContestModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //获取比赛列表
    @Override
    public void getList(String id, String name,final ContestListener listListener) {
        contestService = retrofit.create(ContestService.class);
        Call<List<Contest>> call =contestService.getcontestlist();
        call.enqueue(new Callback<List<Contest>>() {
            @Override
            public void onResponse(Call<List<Contest>> call, Response<List<Contest>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<Contest>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    //获取比赛列表
    @Override
    public void getdetail(String id,final ContestDetailListener listListener) {
       contestService = retrofit.create(ContestService.class);
        Call<Contest> call =contestService.getcontestById(id);
        call.enqueue(new Callback<Contest>() {
            @Override
            public void onResponse(Call<Contest> call, Response<Contest> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Contest> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

}



